package jdbc.note8_2;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class InsertStu {

	/*
	 * 在控制台输入一个学生的全部信息，保存到数据库，注意日期的处理
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入姓名：");
		String sname = in.next();
		System.out.println("请输入性别：");
		String sex = in.next();
		System.out.println("输入电话号码：");
		String telephone = in.next();
		System.out.println("输入qq：");
		String qq = in.next();
		System.out.println("输入微信：");
		String wechat = in.next();
		System.out.println("输入班级编号：");
		int gradeid = in.nextInt();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		System.out.println("输入入学日期：");
		String enterdate = in.next();
		System.out.println("输入籍贯：");
		String fromcity = in.next();
		System.out.println("输入生日：");
		String birthday = in.next();
		Timestamp edate = null;
		Timestamp bdate = null;
		try {
			edate = new Timestamp(sdf.parse(enterdate).getTime());
			bdate = new Timestamp(sdf.parse(birthday).getTime());
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("输入现居地：");
		String address = in.next();
		System.out.println("输入密码：");
		int pass = in.nextInt();
		String sql = "insert into s_student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address,pass)"
				+ " values(?,?,?,?,?,?,?,?,?,?,?)";
		int flag = 0;
		try {
			PreparedStatement pstm = DB.getPstm(sql);
			pstm.setString(1, sname);
			pstm.setString(2, sex);
			pstm.setString(3, telephone);
			pstm.setString(4, qq);
			pstm.setString(5, wechat);
			pstm.setInt(6, gradeid);
			pstm.setTimestamp(7, edate);
			pstm.setString(8, fromcity);
			pstm.setTimestamp(9, bdate);
			pstm.setString(10, address);
			pstm.setInt(11, pass);
			flag = pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close();
		}
		System.out.println("成功更新" + flag + "条数据");
	}
}
